<?php
/**
 * QPay.class.php
 *
 * qq钱包支付相关
 *
 * @author camfee <camfee@foxmail.com>
 * @date   18-1-17 下午6:36
 *
 */

namespace lib\plugins\qpay;

require_once(__DIR__ . '/qpayMch.config.php');
require_once(__DIR__ . '/qpayMchAPI.class.php');

class QPay
{
    /**
     * 获取配置
     */
    private static function getConfig()
    {
        static $config;
        if (empty($config)) {
            $config = loadconf('payment/payment')['qpay'];
        }
        \QpayMchConf::$APP_ID = $config['AppId'];
        \QpayMchConf::$MCH_ID = $config['MchId'];
        \QpayMchConf::$MCH_KEY = $config['MchKey'];
        \QpayMchConf::$CERT_FILE_PATH = $config['CertFilePath'];
        \QpayMchConf::$KEY_FILE_PATH = $config['KeyFilePath'];
        \QpayMchConf::$NOTIFY_URL = $config['NotifyUrl'];
    }

    /**
     * 获取请求实例
     *
     * @param      $url
     * @param null $isSSL
     * @param int  $timeout
     * @return \QpayMchAPI
     */
    private static function getPayApi($url, $isSSL = null, $timeout = 10)
    {
        return new \QpayMchAPI($url, $isSSL, $timeout);
    }

    /**
     * 统一下单
     *
     * @param $params
     * @return array|bool|mixed|object
     */
    public static function unified($params)
    {
        self::getConfig();
        $request = [
            'out_trade_no' => '',
            'body' => '',
            'total_fee' => '1',
            'spbill_create_ip' => '127.0.0.1',
            'trade_type' => 'APP'
        ];
        if (count(array_diff_key($request, $params)) > 0) {
            return false;
        }
        $param["appid"] = \QpayMchConf::$APP_ID;
        $param["mch_id"] = \QpayMchConf::$MCH_ID;
        $param["fee_type"] = "CNY";
        $param["notify_url"] = \QpayMchConf::$NOTIFY_URL;
        $params = array_merge($param, $params);
        //api调用
        $url = 'https://qpay.qq.com/cgi-bin/pay/qpay_unified_order.cgi';
        $ret = self::getPayApi($url)->reqQpay($params);

        return \QpayMchUtil::xmlToArray($ret);
    }

    /**
     * 查询订单
     *
     * @param $params
     * @return array|bool|mixed|object
     */
    public static function query($params)
    {
        self::getConfig();
        $request = [
            'out_trade_no' => '',
        ];
        if (count(array_diff_key($request, $params)) > 0) {
            return false;
        }
        $param["appid"] = \QpayMchConf::$APP_ID;
        $param["mch_id"] = \QpayMchConf::$MCH_ID;
        $params = array_merge($param, $params);
        //api调用
        $url = 'https://qpay.qq.com/cgi-bin/pay/qpay_order_query.cgi';
        $ret = self::getPayApi($url)->reqQpay($params);

        return \QpayMchUtil::xmlToArray($ret);
    }

    /**
     * @param       $prepayId
     * @return array
     */
    public static function generatePackageParams($prepayId)
    {
        $params = [
            'appId' => \QpayMchConf::$APP_ID . '',
            'nonce' => \QpayMchUtil::createNoncestr(),
            'tokenId' => $prepayId,
            'bargainorId' => \QpayMchConf::$MCH_ID . '',
            'pubAcc' => '',
        ];
        $params['timeStamp'] = time() . '';
        $params['sigType'] = 'HMAC-SHA1';
        $params['pubAccHint'] = '';
        $params['sig'] = \QpayMchUtil::getSign($params);


        return $params;
    }

    /**
     * 验证签名
     *
     * @param $params
     * @return string
     */
    public static function checkSign($params)
    {
        $sign = $params['sign'];
        unset($params['sign']);

        return $sign === \QpayMchUtil::getSign($params);
    }

    /**
     * @param $xml
     * @return array|mixed|object
     */
    public static function xmlToArray($xml)
    {
        return \QpayMchUtil::xmlToArray($xml);
    }

    /**
     * @param string $code
     * @param string $msg
     * @return string
     */
    public static function replayNotify($code = 'SUCCESS', $msg = '')
    {
        $arr['return_code'] = $code;
        if (!empty($msg)) {
            $arr['return_msg'] = $msg;
        }


        return \QpayMchUtil::arrayToXml($arr);
    }
}